package com.css.security.core.social.wechat.connect;

import com.css.security.core.social.wechat.api.WeChat;
import com.css.security.core.social.wechat.api.WeChatUserInfo;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.ConnectionValues;
import org.springframework.social.connect.UserProfile;

/**
 * WeChatAdapter
 * 微信 api适配器，将微信 api的数据模型转为spring social的标准模型。
 *
 * @author hanyx
 * @date 2019/04/14
 */
public class WeChatAdapter implements ApiAdapter<WeChat> {

    private String openId;

    public WeChatAdapter() {}

    public WeChatAdapter(String openId){
        this.openId = openId;
    }


    /**
     * api是否可用
     * @param api
     * @return
     */
    @Override
    public boolean test(WeChat api) {
        return true;
    }

    /**
     * 服务提供商用户数据和spring social标准数据结构之间适配
     * @param api
     * @param values
     */
    @Override
    public void setConnectionValues(WeChat api, ConnectionValues values) {
        WeChatUserInfo userInfo = api.getWeChatUserInfo(openId);
        values.setProviderUserId(userInfo.getOpenid());
        values.setDisplayName(userInfo.getNickname());
        values.setImageUrl(userInfo.getHeadimgurl());
    }

    @Override
    public UserProfile fetchUserProfile(WeChat api) {
        return null;
    }

    @Override
    public void updateStatus(WeChat api, String message) {
        // do nothing
    }
}
